<template>
  <sc-dialog title="人员信息列表" v-model="visible" width="65%" destroy-on-close @closed="$emit('closed')">
    <el-container>
      <el-aside width="220px" class="departmentList" v-loading="showGrouploading">
        <el-container>
          <el-header>
            <el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
          </el-header>
          <el-main class="nopadding">
            <el-tree ref="group" class="menu" node-key="id" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick">
            </el-tree>
          </el-main>
        </el-container>
      </el-aside>
      <el-container>
        <el-header class="searchBox">
          <el-form :inline="true" class="form-inline">
            <el-form-item>
              <!-- label="模糊查询" -->
              <el-input v-model="params.keywords" placeholder="用户信息" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleSearch()">查询</el-button>
            </el-form-item>
          </el-form>
        </el-header>
        <el-main class="nopadding">
          <scTable ref="table" :apiObj="listData" v-bind:params="params" row-key="id" @select="handleSelect" stripe border remoteSort>
            <el-table-column type="selection" width="50"></el-table-column>
            <el-table-column label="登录账号" prop="userName" width="150" sortable></el-table-column>
            <el-table-column label="真实姓名" prop="realName" width="150"></el-table-column>
            <el-table-column label="手机号码" prop="telphone" width="120"></el-table-column>
            <el-table-column label="性别" prop="gender" width="70"></el-table-column>
            <el-table-column label="排序" prop="sort" width="80"></el-table-column>
            <el-table-column label="角色" prop="roleNames" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="部门" prop="departmentName" width="200" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="岗位" prop="postNames" width="200" :show-overflow-tooltip="true"></el-table-column>
          </scTable>
        </el-main>
      </el-container>
    </el-container>
    <template #footer>
      <el-button @click="visible=false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">确  定</el-button>
    </template>
  </sc-dialog>
</template>
<script>

export default {
  name: 'userlist',
  components: {
  },
  emits: ['success'],
  data() {
    return {
      visible: false,
      isSaveing: false,
      showGrouploading: false,
      groupFilterText: '',
      group: [],
      groupsProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        emitPath: false,
        checkStrictly: true
      },
      params: {
        keywords: "",
        departmentId: null,
      },
      listData: [],
      selectionIds: [],
    }
  },
  watch: {
    groupFilterText(val) {
      this.$refs.group.filter(val);
    }
  },
  mounted() {
  },
  methods: {
    //获取菜单列表信息
    open(userIdList) {
      this.visible = true;
      this.initGroup();
      this.initData(userIdList);
    },
    //初始化信息
    async initData(userIdList) {

      this.selectionIds = [];
      
      var res = await this.$API.user.getPageList.get(this.params);
      if (res.code == 200) {

        this.$refs.table.tableData = res.data.rows;
        this.$refs.table.total = res.data.total;


        if (userIdList.length > 0) {
          this.selectionIds = userIdList;
        }
        let rows = []
        for (let m = 0; m < this.selectionIds.length; m++) {
          for (let n = 0; n < this.$refs.table.tableData.length; n++) {
            if (this.selectionIds[m] === this.$refs.table.tableData[n].id) {
              rows.push(this.$refs.table.tableData[n])
            }
          }
        }

        this.$nextTick(() => {
          this.$refs.table.clearSelection();
          rows.forEach(row => {
            this.$refs.table.toggleRowSelection(row);
          });
        });
      }
    },
    //获取数据
    async handleSearch() {
      this.$refs.table.loadData(this.params);
    },
    //加载树数据
    async initGroup() {
      this.showGrouploading = true;
      var res = await this.$API.department.getTreeList.get();
      this.showGrouploading = false;
      this.group = res.data;
    },
    //树过滤
    groupFilterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    //树点击事件
    groupClick(data) {
      var params = {
        departmentId: data.id
      }
      this.$refs.table.reloadData(params);
    },
    //表格选择后回调事件
    handleSelect(selectRows, item) {
      var index = this.selectionIds.indexOf(item.id);
      if (selectRows.includes(item)) {
        //勾选时做的事
        if (index == -1) {
          this.selectionIds.push(item.id);
        }
      } else {
        //取消勾选时做的事，arguments[1]是当前取消勾选的项
        //console.log(arguments[1].id);
        if (index > -1) {
          this.selectionIds.splice(index, 1);
        }
      }
    },
    //确认信息
    submit(){
      this.$emit('success', this.selectionIds);
      this.visible = false;
    }
  }
}
</script>

<style>
.departmentList {
  border-left: 1px solid #eee;
  overflow-y: auto;
  height: 420px;
}
</style>
